package com.yzd.netmonitor.util;

import java.io.File;

/**
 * 目录文件辅助类.
 * 
 * @author YRJ
 *
 */
public final class FileHelper {

	/**
	 * 创建目录字符串. /path
	 * @param paths
	 * @return
	 */
	public final static String mkdir(final String... paths) {
		final StringBuilder dir = new StringBuilder();
		for (String path : paths) {
			if (path.charAt(0) != '/') {
				dir.append('/');
			}
			dir.append(path);
		}
		return dir.toString();
	}

	/**
	 * 创建目录文件.
	 * 
	 * @param paths
	 * @return
	 */
	public final static File mkdirs(final String... paths) {
		final String dir = mkdir(paths);
		final File file = new File(dir);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	/**
	 * 获取文件后缀名.
	 * 
	 * @param file
	 * @return
	 */
	public final static String suffix(final String file) {
		if (Check.NuNString(file)) {
			return "";
		}

		final int lastIndexOf = file.lastIndexOf('.');
		if (lastIndexOf == -1) {
			return file;
		}

		return file.substring(lastIndexOf + 1);
	}

	/**
	 * 获取文件名.
	 * 
	 * @param file
	 * @return
	 */
	public final static String getName(final String file) {
		if (Check.NuNString(file)) {
			return "";
		}
		final int lastIndexOf = file.lastIndexOf('/');
		if (lastIndexOf == -1) {
			return file;
		}
		return file.substring(lastIndexOf + 1);
	}

	private FileHelper() {
		throw new AssertionError("Uninstantiable class.");
	}
}